﻿using CoreShop.Model;
using Sunny.UI;
using System.Collections.Generic;
using System.Drawing;

namespace CoreShop.App
{
    public partial class UIPractice : UIDropDownItem
    {
        public UIPractice()
        {
            InitializeComponent();
        }

        private void uiSymbolLabel2_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        public void AddType(string type)
        {
            UIMenuItemSeparator item = new UIMenuItemSeparator(type, 40);
            item.SetOtherMode();
            uiFlowLayoutPanel2.Add(item);
        }

        public void AddItems(string type, List<corecmsfoodtask> items, bool autoWidth, Size itemSize, int height, int maxSelected = int.MaxValue)
        {
            UIMultiButtons buttons = new UIMultiButtons();
            buttons.Width = uiFlowLayoutPanel2.Width - 30;
            buttons.Height = height;

            buttons.TagString = type;
            buttons.AutoWidth = autoWidth;
            buttons.MaxSelected = maxSelected;
            buttons.ItemSize = itemSize;
            buttons.OnOverSelectedCount += Buttons_OnOverSelectedCount;
            foreach (var item in items)
            {
                buttons.Add(item.name, item.id, item.selectedFlag);
            }

            uiFlowLayoutPanel2.Add(buttons);
        }

        private void Buttons_OnOverSelectedCount(object sender, System.EventArgs e)
        {
            UIMultiButtons button = (UIMultiButtons)sender;
            UIMessageTip.ShowWarning(button, button.TagString + "只能最多选择" + button.MaxSelected + "项");
        }

        private void uiButton23_Click(object sender, System.EventArgs e)
        {
            var buttons = this.GetControls<UIMultiButtons>(true);

            foreach (UIMultiButtons button in buttons)
            {
                if (button.SelectedCount == 0)
                {
                    UIMessageTip.ShowWarning(button, "请选择" + button.TagString);
                    return;
                }
            }

            DoValueChanged(this, buttons);
            Close();
        }
    }
}
